package cn.new7as.cas5s.ui.record;


import static cn.new7as.cas5s.tool.util.Utils.setLinerarHeight;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import java.util.ArrayList;
import java.util.List;

import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.databinding.ActivityRecordsdetailsBinding;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.RecordDetailsApi;
import cn.new7as.cas5s.ui.easy.netapi.RecordsApi;
import cn.new7as.cas5s.ui.easy.netapi.UtrSendApi;


public class RecordsDetailActivity extends BaseActivity {


    private ActivityRecordsdetailsBinding binding;
    private List<String> mfileDatas = new ArrayList<>();

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRecordsdetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("Details");
        initV();
    }

    @Override
    public void initV() {
        super.initV();
        show();
        getRecord();
    }

    private void getRecord() {
        String ddba = getIntent().getStringExtra("data");
        EasyHttp.get(this)
                .api(new RecordDetailsApi(getunique(), ddba)).request(new OnHttpListener<HttpData<RecordsApi.Data>>() {

                    @Override
                    public void onHttpSuccess(@NonNull HttpData<RecordsApi.Data> result) {
                        hide();
                        RecordsApi.Data result1 = result.getResult();
                        binding.namr.setText(result1.name);
                        binding.whats.setText(result1.whatsup);
                        binding.email.setText(result1.email);
                        binding.content.setText(result1.content);
                        if (result1.replyStatus == 1) {
                            binding.tvres.setVisibility(View.VISIBLE);
                            binding.llres.setVisibility(View.VISIBLE);
                            if (result.getResult().replyList != null)
                                binding.replygride.setAdapter(new ReplyAdapter(result.getResult().replyList, RecordsDetailActivity.this));
                        }
                        List<UtrSendApi.Data> list = new ArrayList<>();
                        if (result.getResult().imageList != null) {
                            List<String> annexList = result.getResult().imageList;

                            for (int i = 0; i < annexList.size(); i++) {
                                UtrSendApi.Data data = new UtrSendApi.Data();
                                data.hjadkvsa = annexList.get(i);
                                list.add(data);
                            }
                            binding.monthGrid.setAdapter(new FeedBackAdapter(list, RecordsDetailActivity.this));


                        }

                        if (result.getResult().annexList != null) {
                            String[] annexList = result.getResult().annexList;

                            for (int i = 0; i < annexList.length; i++) {
                                mfileDatas.add(annexList[i]);
                            }

                        }

                        SendsFileMsgadapter sendsFileMsgadapter = new SendsFileMsgadapter(mfileDatas, RecordsDetailActivity.this, 1);
                        binding.filegride.setAdapter(sendsFileMsgadapter);
                        setListViewHeightBasedOnChildren(binding.filegride);

                        binding.monthGrid.measure(0, 0);
                        int height = binding.lmsg.getMeasuredHeight();
                        int height2 = binding.monthGrid.getMeasuredHeight();
                        binding.content.measure(0, 0);
                        int height3 = binding.content.getMeasuredHeight();
                        Log.e("aaaa", height + "-" + height2 + "-" + height3);
                        if (list.size() == 0 && result.getResult().annexList == null)
                            setLinerarHeight(binding.lmsg, height - 450 + height3);
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }


    public void setListViewHeightBasedOnChildren(ListView listView1) {
        BaseAdapter listAdapter = (BaseAdapter) listView1.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;

        ViewGroup.LayoutParams params = listView1.getLayoutParams();
        int listViewWidth = getWindowManager().getDefaultDisplay().getWidth();
        int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView1);
            listItem.measure(widthSpec, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        params.height = totalHeight
                + (listView1.getDividerHeight() * (listAdapter.getCount() + 1));
        listView1.setLayoutParams(params);
    }
}